//
//  SEGMineHouseListViewModel.m
//  Commom
//
//  Created by 梁学彰 on 2020/6/2.
//

#import "SEGMineHouseListViewModel.h"
#import "SEGRentalPriceManager.h"

@implementation SEGMineHouseListViewModel


- (RACSignal *)sendMineRentalListRequest {
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @weakify(self);
        [self.requestManager requestMineRentalListWithFinished:^(id result, NSError *error) {
            @strongify(self);
            if (!error) {
                if ([result isKindOfClass:[NSDictionary class]]) {
                    NSDictionary *dict = result;
                    if (dict[@"data"]&&[dict[@"data"] isKindOfClass:[SEGNewMineRentallistDataModel class]]) {
                        SEGNewMineRentallistDataModel *model = dict[@"data"];
                        self.mineRentalArray = [model.myRentalList mutableCopy];
                        [subscriber sendNext:nil];
                    }
                }
            }else {

                [subscriber sendError:error];
            }
            [subscriber sendCompleted];
        }];
        return nil;
    }];
    return signal;
    
    
}

- (RACSignal *)sendDeleteRequestWithRentalId:(NSString *)rentalId {
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @weakify(self);
        [self.requestManager requestDeleteMineHouseWithRentalId:rentalId finished:^(id result, NSError *error) {
            @strongify(self);
            if (!error) {
                //成功之后 删除那行数据
                NSPredicate *predicate = [NSPredicate predicateWithFormat:@"rentalId == %@", rentalId];
                NSArray <SEGNewRentallistitemModel *> *deleteArr = [self.mineRentalArray filteredArrayUsingPredicate:predicate];
                if (deleteArr.count > 0 &&[self.mineRentalArray containsObject:deleteArr.firstObject]) {
                    NSInteger index = [self.mineRentalArray indexOfObject:deleteArr.firstObject];
                    [self.mineRentalArray removeObjectAtIndex:index];
                    [subscriber sendNext:[NSNumber numberWithInteger:index]];
                }
            }else {
                [subscriber sendError:error];
                //失败了 就转圈圈重新请求
                [self sendMineRentalListRequest];
            }
            [subscriber sendCompleted];
        }];
        return nil;
    }];
    return signal;
}
//下架
- (RACSignal *)sendoffLineRequestWithRentalId:(NSString *)rentalId {
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [self.requestManager requestOffLineMineHouseWithRentalId:rentalId releaseType:@"5" finished:^(id result, NSError *error) {
            if (!error) {
                //成功之后 刷新数据
                [subscriber sendNext:nil];
            }else {
                [subscriber sendError:error];
            }
            [subscriber sendCompleted];
        }];
        return nil;
    }];
    return signal;
}

//擦亮
- (RACSignal *)sendShineRequestWithRentalId:(NSString *)rentalId {
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [self.requestManager requestShineMineHouseWithRentalId:rentalId  finished:^(id result, NSError *error) {
            if (!error) {
                //成功之后 刷新数据
                [subscriber sendNext:nil];
            }else {
                [subscriber sendError:error];
            }
            [subscriber sendCompleted];
        }];
        return nil;
    }];
    return signal;
}

- (void)shareActionWithModel:(SEGNewRentallistitemModel *)model viewController:(UIViewController *)viewController {
    NSString* url = [NSString stringWithFormat:@"%@/h5/rent-sale-sharepage/?#/rentSale?rentalId=%@&rentalType=%@&app_platform_code=%@",kClientRootURL,model.rentalId,model.rentalType,kAppPlatformCode];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"shareImageUrlString"] = [NSString stringWithFormat:@"%@%@",kClientImageRootURL,[model.housePhoto componentsSeparatedByString:@","].firstObject];
    param[@"sharetitle"] = model.rentalTitle;
    if (model.rentalType.integerValue == 4) {
        param[@"sharecontent"] = [NSString stringWithFormat:@"%@室%@厅%@卫%@m²%@万",model.room,model.hall,model.toilet,[SEGRentalPriceManager getPriceStringWithformatValue:model.area],[SEGRentalPriceManager getPriceStringWithformatValue:model.price]];
    }else {
        param[@"sharecontent"] = [NSString stringWithFormat:@"%@室%@厅%@卫%@m²%@元/月",model.room,model.hall,model.toilet,[SEGRentalPriceManager getPriceStringWithformatValue:model.area],[SEGRentalPriceManager getPriceStringWithformatValue:model.price]];
    }
    
    param[@"inviteurl"] = url ?: @"";
    
    UIViewController *shareVC = [[SEGUhomeMediator sharedInstance] seg_initShareViewController:param actionType:^(NSInteger actionType) {
    } channel:^(NSString *channel) {
        
    }];
    if (shareVC) {
        [viewController presentViewController:shareVC animated:NO completion:nil];
    }
}



- (SEGHouseRentalAndSaleRequestManager *)requestManager {
    if (!_requestManager) {
        _requestManager = [[SEGHouseRentalAndSaleRequestManager alloc] init];
    }
    return _requestManager;
}
@end
